// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({env: cloud.DYNAMIC_CURRENT_ENV})
const db = cloud.database()
const _ = db.command
const dbTask = db.collection('task')

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    //根据taskID获取任务
    var taskGet = await dbTask.where({_id:event._id}).get()
    if(taskGet['errMsg'] !== "collection.get:ok"){
      return null
    }
    var task = await taskGet['data'][0]
    //获取任务所有信息
    if(event.action == "getAll"){
      return task
    }
    //获取发布人信息，包含ID和昵称
    else if(event.action == "getPInfo"){
      return task['publisherInfo']
    }
    //获取接收人信息，包含ID和昵称
    else if(event.action == "getRInfo"){
      return task['receiverInfo']
    }
    //获取出发地
    else if(event.action == "getFrom"){
      return task['from']
    }
    //获取目的地
    else if(event.action == "getTo"){
      return task['to']
    }
    //获取价格
    else if(event.action == "getPrice"){
      return task['price']
    }
    //获取任务状态
    else if(event.action == "getState"){
      return task['state']
    }
    //获取任务详情
    else if(event.action == "getContent"){
      return task['content']
    }
}
